<?php


namespace app\api\controller;

use app\common\bean\ListMap;
use app\common\bean\OrderGoodsBean;
use app\common\bean\UserBean;
use app\common\bean\UserImageBean;
use app\common\controller\ApiController;
use app\common\enum\OrderGoodsStatusEnum;
use app\common\manage\OrderGoodsManage;
use app\common\manage\UserManage;
use app\common\model\OrderGoodsModel;
use app\common\model\UserImageModel;
use app\common\utils\CommonUtil;
use app\common\utils\ExitJsonUtil;
use think\Db;

class Order extends ApiController
{
    protected $beforeActionList = [
        'decryptToken' => ['except' => 'callBack'],
    ];
    public function clearImgs(){//自动清理一周前的照片
        $files = ['upload/userImg/', 'upload/office/'];
        foreach ($files as $furl){
            $dirs = scandir( $furl );
            unset($dirs[0]);unset($dirs[1]);
            foreach ($dirs as $d){
                if( $d<date('YmW') ){
                    $dir = $furl.$d;
                    $imgs = scandir( $dir );
                    unset($imgs[0]);unset($imgs[1]);
                    foreach ( $imgs as $i ) unlink($dir.'/'.$i);
                    rmdir( $dir );
                }
            }
        }

        $day = date('Y-m-d', strtotime('-1 week'));
        $where[] = [UserImageBean::$createdAt,'LT', $day];
        UserImageModel::getInstance()->where($where)->delete();
    }
    public function callBack(){//支付回调
        $cbData = CommonUtil::getInstance()->backData();
        //file_put_contents('callback.txt', date('Y-m-d H:i:s')."-{$cbData['out_trade_no']}\n", FILE_APPEND);
        $order = OrderGoodsModel::getInstance()->selectData([OrderGoodsBean::$orderNumber=>$cbData['out_trade_no']]);
        if($order[OrderGoodsBean::$status]==OrderGoodsStatusEnum::NOPAY){
            $userBean = new UserBean();
            $userBean->setId($order[OrderGoodsBean::$userId]);
            $userBean = UserManage::getInstance()->getInfo($userBean);
            $orderBean = new OrderGoodsBean();
            $orderBean->setData( $order );
            $orderBean->setParameter('blance', $userBean->getBlance());
            OrderGoodsManage::getInstance()->payBack( $orderBean );
            ExitJsonUtil::getInstance()->exitData([]);
        }else{
            ExitJsonUtil::getInstance()->exitData($order, 501, '已支付');
        }
    }
    public function pay(){//订单支付
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $listMap->setParameter('uid', $this->userId);
        $listMap->setParameter('openid', $this->openId);
        $res = OrderGoodsManage::getInstance()->pay($listMap);
        ExitJsonUtil::getInstance()->exitData($res);
    }
    public function detail(){//订单详情
        $orderGoodsBean = new OrderGoodsBean();
        $orderGoodsBean->setData($this->postParams);
        $orderGoodsBean = OrderGoodsManage::getInstance()->getInfo($orderGoodsBean);
        ExitJsonUtil::getInstance()->exitData($orderGoodsBean->getData());
    }
    public function index(){//订单列表
        $listMap = new ListMap();
        $listMap->setParameter(OrderGoodsBean::$userId, $this->userId);
        $orderListMap = OrderGoodsManage::getInstance()->getDetail($listMap);
        $data['list'] = $orderListMap->getList();
        $data['currentPage'] = $orderListMap->getCurrentPage();
        $data['totalCount'] = $orderListMap->getTotalCount();
        ExitJsonUtil::getInstance()->exitData( $data );
    }

    public function create(){//创建订单
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $listMap->setParameter(OrderGoodsBean::$userId, $this->userId);
        $orderGoodsBean = OrderGoodsManage::getInstance()->create($listMap);
        ExitJsonUtil::getInstance()->exitData($orderGoodsBean->getData());
    }
    public function getFreight(){//获取运费
        $userBean = new UserBean();
        $userBean->setId($this->userId);
        $userBean = UserManage::getInstance()->getInfo( $userBean );
        $freight = OrderGoodsManage::getInstance()->calPostage($this->postParams['user_address_id'],$userBean->getStoreId());
        ExitJsonUtil::getInstance()->exitData($freight);
    }

    /**
     * 下单页面计算商品列表的价格
     */
    public function getOrderDetailPrices(){
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $listMap->setParameter(OrderGoodsBean::$userId, $this->userId);
        list($result, $ismsg) = OrderGoodsManage::getInstance()->getOrderDetailPrices($listMap);
        ExitJsonUtil::getInstance()->exitData($result, 200, $ismsg);
    }
}